package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfMerAccService.java
* Author:   you.yichao
* Date:     2019-07-08
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-07-08          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.cache.sequence.util.SequenceGenerator;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.enums.PlatformRetCodeEnum;
import com.mdb.open.common.enums.TfSequenceEnum;
import com.mdb.open.common.enums.trade.status.CusAccStatusEnum;
import com.mdb.open.common.enums.trade.status.MerAccStatusEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.GetMerAccBo;
import com.mdb.open.trade.domain.bo.trade.SaveMerAccBo;
import com.mdb.open.trade.domain.po.trade.CusAcc;
import com.mdb.open.trade.domain.po.trade.MerAcc;
import com.mdb.open.trade.domain.vo.trade.MerchantPlatformAccVo;
import com.mdb.open.trade.mapper.trade.TfCusAccMapper;
import com.mdb.open.trade.mapper.trade.TfMerAccMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  融资商户子账户表 服务类
 *
 * @author you.yichao
 * @date 2019-07-08
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfMerAccService extends SuperService<TfMerAccMapper, MerAcc> {

    @Autowired
    SequenceGenerator sequenceGenerator;

    @Autowired
    TfCusAccMapper tfCusAccMapper;

    @Autowired
    MerSubAccAutoIncrementInitializer merSubAccAutoIncrementInitializer;


    public ResultEx<MerAcc> getByMerchantNo(GetMerAccBo bo) {
        return ResultEx.ok(this.baseMapper.selectOne(new LambdaQueryWrapper<MerAcc>()
                .eq(MerAcc::getCustomerCode, bo.getCustomerCode())
                .eq(MerAcc::getMerchantNo, bo.getMerchantNo())
                .eq(MerAcc::getStatus, MerAccStatusEnum.NORMAL.getValue())));
    }

    public ResultEx<MerAcc> getByAcc(String acc) {
        return ResultEx.ok(this.baseMapper.selectOne(new LambdaQueryWrapper<MerAcc>()
                .eq(MerAcc::getPlatformAcc, acc)
                .eq(MerAcc::getStatus, MerAccStatusEnum.NORMAL.getValue())));
    }

    public ResultEx<MerchantPlatformAccVo> saveMerAcc(SaveMerAccBo bo) {

        CusAcc cusAcc = tfCusAccMapper.selectOne(new LambdaQueryWrapper<CusAcc>()
                .eq(CusAcc::getCustomerCode, bo.getCustomerCode())
                .eq(CusAcc::getStatus, CusAccStatusEnum.NORMAL));
        if (ObjectUtil.isEmpty(cusAcc) || ObjectUtil.isEmpty(cusAcc.getDaAcc())) {
            return ResultEx.makeResult(PlatformRetCodeEnum.CUS_ACC_NOT_EXIST);
        }

        MerAcc acc = this.baseMapper.selectOne(new LambdaQueryWrapper<MerAcc>()
                .eq(MerAcc::getMerchantNo, bo.getMerchantNo())
                .eq(MerAcc::getCustomerCode, bo.getCustomerCode()));
        if (ObjectUtil.isNotEmpty(acc)) {
            MerchantPlatformAccVo vo = new MerchantPlatformAccVo();
            vo.setMerchantPlatformAcc(acc.getPlatformAcc());
            return ResultEx.ok(vo);
        }

        MerAcc saveMerAcc = new MerAcc();
        saveMerAcc.setCustomerCode(bo.getCustomerCode());
        saveMerAcc.setCustomerName(bo.getCustomerName());
        saveMerAcc.setMerchantNo(bo.getMerchantNo());
        saveMerAcc.setMerchantName(bo.getMerchantName());
        String merchantAcc = cusAcc.getDaAcc() + sequenceGenerator.generator(TfSequenceEnum.MERCHANT_ACC_NO, merSubAccAutoIncrementInitializer);
        saveMerAcc.setPlatformAcc(merchantAcc);
        saveMerAcc.setStatus(MerAccStatusEnum.NORMAL);


        if (this.baseMapper.insert(saveMerAcc) != 1) {
            return ResultEx.fail();
        }
        MerchantPlatformAccVo vo = new MerchantPlatformAccVo();
        vo.setMerchantPlatformAcc(merchantAcc);

        return ResultEx.ok(vo);
    }

}
